
from Patch import *
X = 0
Y = 1


class Grid2D:
    
    def __init__(self,SCREEN,grid_size,g):
        self.grid = {}
        self.grid_size = grid_size
        
        #Criar grelha
        for y in range(grid_size[Y]):
            for x in range(grid_size[X]):
                self.grid[(x,y)]=(Patch(SCREEN,(x,y),g))
        
        #Definir vizinhanca
        for p in self.grid.itervalues():
            lst = []
            coords = p.genCoordNeigh8(grid_size)
            for coord in coords:
                lst.append(self.getPatch(coord))
            p.setNeigh8(set(lst))
            p.setNeigh4(set((lst[1],lst[3],lst[4],lst[6])))
            p.setNeigh4X(set((lst[0],lst[2],lst[5],lst[7])))
            p.neigh2H = set((lst[3],lst[4]))
            p.neigh2V = set((lst[1],lst[6]))
            p.N = lst[1]
            p.S = lst[6]
            p.E = lst[4]
            p.O = lst[3]
            p.NE = lst[2]
            p.SE = lst[7]
            p.NO = lst[0]
            p.SO = lst[5]
            
        for p in self.grid.itervalues():
            lst = []
            for n in p.getNeigh8():
                lst.extend(n.getNeigh8())
            lst = set(lst)
            lst.discard(p)
            p.setNeigh16(lst)
            
        
    
    def getGrid(self):
        return self.grid        
        
    def getPatch(self,coord):
        return self.grid[coord]
    
   